package com.gzx.plugin.tjzy.modular.sms.batch.mapper;


import com.gzx.plugin.tjzy.modular.sms.batch.entity.GzxSmsDetail;
import com.gzx.plugin.tjzy.modular.sms.batch.entity.GzxSmsDetailCount;
import com.gzx.plugin.tjzy.modular.sms.batch.vo.GzxSmsDetailVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import vip.xiaonuo.common.mapper.BaseMapperPlus;

import java.util.List;

/**
 * @author zjt
 * @description
 * @since 2024/9/23 下午4:01
 */
public interface GzxSmsDetailMapper extends BaseMapperPlus<GzxSmsDetail, GzxSmsDetailVo> {


    @Insert("<script> " +
            "INSERT INTO gzx_sms_detail (batch_number, project_code,sign_city,to_phone, supplier_order, item_json, template_code, message_content, tenant_id, create_user, create_time) " +
            "VALUES ${valSql}" +
            "</script>")
    void insertBatchSql(String valSql);

    @Update("<script>" +
            "UPDATE gzx_sms_detail " +
            "SET send_status = CASE id " +
            "<foreach collection='list' item='item' separator=''>" +
            "WHEN #{item.id} THEN #{item.sendStatus}" +
            "</foreach> " +
            "END, " +
            "supplier_code = CASE id " +
            "<foreach collection='list' item='item' separator=''>" +
            "WHEN #{item.id} THEN #{item.supplierCode}" +
            "</foreach> " +
            "END, " +
            "message_id = CASE id " +
            "<foreach collection='list' item='item' separator=''>" +
            "WHEN #{item.id} THEN #{item.messageId} " +
            "</foreach> " +
            "END," +
            "send_time = CASE id " +
            "<foreach collection='list' item='item' separator=''>" +
            "WHEN #{item.id} THEN #{item.sendTime}" +
            "</foreach> " +
            "END " +
            "WHERE id IN " +
            "<foreach collection='list' item='item' open='(' separator=',' close=')'>" +
            "#{item.id}" +
            "</foreach>" +
            "</script>")
    void saveMsgId(List<GzxSmsDetail> smsDetailList);

    @Select("<script>" +
            "select batch_number, " +
            "SUM(CASE WHEN send_status = 2 THEN 1 ELSE 0 END) AS successQuantity," +
            "SUM(CASE WHEN send_status = 3 THEN 1 ELSE 0 END) AS failQuantity," +
            "SUM(CASE WHEN send_status = 0 THEN 1 ELSE 0 END) AS noSendQuantity " +
            "from gzx_sms_detail " +
            "where batch_number in " +
            "<foreach item='batchNumber' collection='batchNumberList' open='(' separator=',' close=')'>" +
            "#{batchNumber}" +
            "</foreach> " +
            "group by batch_number " +
            "</script>")
    List<GzxSmsDetailCount> countByBatchNumber(List<String> batchNumberList);
}
